﻿

function GetGoodsType() {
    $.get("/Goods/GetGoodsType").then((res) => {

        Peng_Select.CreateElement('GoodsType', res.data, 'name', (res) => {
            GetGoodsLabel(res.id);
        });
    });
}
GetGoodsType();

let HasFlovar = [
    {
        name: "无",
        height: '0px',
        overflow: 'hidden',
    }, {
        name: "有",
        height: '40px',
        overflow: 'initial'
    }
];

let FlovarData = null;


function GetGoodsLabel(GTid) {
    $.get("/Goods/GetGoodslabelByGTid", { id: GTid }).then(res => {
        Peng_Select.CreateElement('GoodsLabel', res.data, 'name', (res) => {
            Good.Glid = res.id;

        });



        Peng_Select.CreateElement('IsFlavorType', HasFlovar, 'name', (res) => {


            document.querySelector("#FlavorTypes").style.height = res.height;
            document.querySelector("#FlavorTypes").style.overflow = res.overflow;

            if (res.name == '无') {
                FlovarData = null;
            } else {
                FlovarData = 1111;
            }


        });

        getFlavorType();
    });
}


function getFlavorType() {
    $.get("/Goods/GetFlavorType").then((res) => {
        Peng_Select.CreateElement('FlavorType', res.data, 'name', (res) => {
            Good.Ftid = res.id;
            BindFlavor(FlovarData);
        });

    });

}


function BindFlavor(FlovarData) {
    let flavorBox = document.querySelector(".flavorBox");

    flavorBox.innerHTML = "";
    if (FlovarData != null) {
        $.get("/Goods/GetFlavor", { ftid: Good.Ftid }).then((res) => {
            console.log(res.data);
            res.data.forEach((item, i) => {
                let flavor = document.createElement("div");
                flavor.className = "flavor";
                flavor.innerHTML = item.name;
                flavorBox.appendChild(flavor);
            });

        });
    }
}




let imgFile = document.querySelector("#imgFile");
let imgBox = document.querySelector(".img");
let Src = document.querySelector(".Src");
imgFile.addEventListener("change", function () {
    let img = document.createElement("img");

    if (imgFile.value != "") {
        imgBox.innerHTML = "";
        img.src = window.URL.createObjectURL(imgFile.files[0]);
        imgBox.appendChild(img);
    } else {
        imgBox.innerHTML = "你还没有上传图片";
    }
    Src.innerHTML = imgFile.value;


});
function fileUpLoad() {
    imgFile.click();
}

let inputs = document.querySelectorAll(".Form .left input");
let textarea = document.querySelector(".Form .left textarea");

let Good = {
    name: '',
    details: '',
    price: 0,
    Glid: '',
    num: 0,
    Ftid: null
};


function AddGood() {


    if (inputs[0].value != "" && inputs[1].value != "" && inputs[2].value != "") {

        Good.name = inputs[0].value;
        Good.price = inputs[1].value;
        Good.num = inputs[2].value;
        Good.details = textarea.value;

        if (FlovarData == null) {
            Good.Ftid = null;
        }




        if (imgFile.value == "") {
            Peng.Show("你还没有上传图片", { type: 'Danger' });

            imgFile.click();
        } else {


            let hzm = imgFile.value.substring(imgFile.value.lastIndexOf('.') + 1);

            console.log(hzm);
            if (hzm == "png" || hzm == "jpg" || hzm == "gif") {

                var formData = new FormData();

                formData.append('img', imgFile.files[0]);
                formData.append('goods', JSON.stringify(Good));


                $.ajax({
                    url: '/Goods/AddGoods',
                    type: 'POST',
                    dataType: 'json',
                    data: formData,
                    cache: false,
                    traditional: true,
                    contentType: false,
                    processData: false,
                    success: function (data) {


                        if (data.code == 0) {
                            Peng.Show(data.message, { type: 'Success' });

                            window.location.href = "/Goods/AddGoodsHtml";



                        } else {
                            Peng.Show(data.message, { type: 'Danger' });

                        }

                    },
                    error: function () {

                    }

                });



            } else {
                Peng.Show("只允许 'png' 和 'jpg' 格式的图片", { type: 'Danger' });
            }

        }




    } else {
        Peng.Show("数据不能为空", { type: 'Danger' });


        for (let i = 0; i < inputs.length; i++) {
            if (inputs[i].value == "") {
                inputs[i].focus();
                break;
            }
        }



    }
}